package root.electronclass.electronclass.ext

import android.text.SpannedString
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 *
 * @author Lokiy
 * @date 2023/2/4 15:01
 */

val yyyyMMddHHmm = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
val yyyyMMddHHmmss = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
val yyyyMMdd = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
val MMdd = SimpleDateFormat("MM-dd", Locale.CHINA)
val HHmm = SimpleDateFormat("HH:mm", Locale.CHINA)
val EEEE = SimpleDateFormat("EEEE", Locale.CHINA)
val _yyyyMMddHHmm = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.CHINA)

fun Long.toDateYyyyMMddHHmm(): String {
    return yyyyMMddHHmm.format(Date(this))
}

fun Long.toDateYyyyMMddHHmmss(): String {
    return yyyyMMddHHmmss.format(Date(this))
}

fun Long.toDateYyyyMMdd(): String {
    return yyyyMMdd.format(Date(this))
}

fun Long.toDateMMdd(): String {
    return MMdd.format(Date(this))
}

fun Long.toDateHHmm(): String {
    return HHmm.format(Date(this))
}

fun Long.toEEEE(): String {
    return EEEE.format(Date(this))
}

fun String.toEEEE(simpleDateFormat: SimpleDateFormat): String {
    return kotlin.runCatching {
        EEEE.format(simpleDateFormat.parse(this))
    }.getOrElse { "" }
}

fun String.toMMdd(simpleDateFormat: SimpleDateFormat): String {
    return kotlin.runCatching {
        MMdd.format(simpleDateFormat.parse(this))
    }.getOrElse { "" }
}

/**
 * 将时间字符串转换为 SpannedString 格式，显示为 MM-dd\nHH:mm
 * @receiver String 时间字符串，格式为 yyyy-MM-dd HH:mm:ss
 * @return SpannedString 格式的时间字符串
 */
val String.toMMddnHHmm: SpannedString
    get() {
        val createTime = this
        if (createTime.isEmpty() || createTime.length != 19) return buildSpannedString { append(createTime) }
        return buildSpannedString {
            bold {
                append(createTime.substring(5, 10)) // MM-dd
            }
            append("\n")
            scale(0.583f) {
                append(createTime.substring(11, 16)) // HH:mm
            }
        }
    }

/**
 * 将时间字符串转换为 SpannedString 格式，显示为 MM-dd\nyyyy
 * @receiver String 时间字符串，格式为 yyyy-MM-dd
 * @return SpannedString 格式的时间字符串
 */
val String.toMMddnyyyy: SpannedString
    get() {
        val createTime = this
        if (createTime.isEmpty() || createTime.length != 10) return buildSpannedString { append(createTime) }
        return buildSpannedString {
            bold {
                append(createTime.substring(5, 10)) // MM-dd
            }
            append("\n")
            scale(0.583f) {
                append(createTime.substring(0, 4)) // HH:mm
            }
        }
    }


/**
 * 将时间字符串转换为 SpannedString 格式，显示为 MM-dd
 * @receiver String 时间字符串，格式为 yyyy-MM-dd
 * @return SpannedString 格式的时间字符串
 */
val String.toMMdd: SpannedString
    get() {
        val createTime = this
        if (createTime.isEmpty() || createTime.length != 10) return buildSpannedString { append(createTime) }
        return buildSpannedString {
            append(createTime.substring(5, 10)) // MM-dd
        }
    }